package com.xcf.controller.business.settlement;

import cn.com.xcf.dao.model.InvoiceTitle;
import cn.com.xcf.dao.model.InvoiceTitleExample;
import cn.com.xcf.service.InvoiceTitleService;
import com.xcf.controller.base.BaseController;
import com.xcf.controller.base.CommonResponse;
import com.xcf.entity.Page;
import com.xcf.entity.system.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * InvoiceTitleController
 * @author WongBin
 * @date 2017/12/8.
 */
@Controller
@RequestMapping("/invoiceTitleManage")
public class InvoiceTitleController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(InvoiceTitleController.class);
    private static final String PREFIX = "/business/finance/";
    @Resource
    private InvoiceTitleService invoiceTitleService;

    @InitBinder
    public void initBinder(WebDataBinder binder){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
    }

    /**
    * save
    * @param
    * @throws Exception
    */
    @RequestMapping(value="/save")
    @ResponseBody
    public Object save(InvoiceTitle data,
                       HttpServletRequest request,
                       HttpServletResponse response) throws Exception{
        ModelAndView mv = this.getModelAndView();
        User user = getLoginUser(request,response);
        data.setTitle(data.getSettlementCompany());
        data.setName("");
        if(data.getCrtDate()==null) {
            data.setCrtDate(new java.util.Date());
        }
        data.setCreateBy(user.getUSER_ID());
        data.setCreateTime(new Date());
        invoiceTitleService.insertSelective(data);
        //mv.addObject("msg","success");
        //mv.setViewName("save_result");

        return CommonResponse.success();
    }

    /**删除
    * @throws Exception
    */
    @RequestMapping(value="/delete")
    @ResponseBody
    public CommonResponse delete(String ids) throws Exception{
        InvoiceTitleExample ex = new InvoiceTitleExample();
        List<Long> idList = new ArrayList(ids.split(",").length);
        for(String id : ids.split(",")){
            idList.add(Long.valueOf(id));
        }
        ex.createCriteria().andIdIn(idList);
        invoiceTitleService.deleteByExample(ex);
        return CommonResponse.success();
    }

    /**修改
    * @param
    * @throws Exception
    */
    @RequestMapping(value="/edit")
    public ModelAndView edit() throws Exception{

        return null;
    }

    /**列表
    * @param page
    * @throws Exception
    */
    @RequestMapping(value="/list")
    public ModelAndView list(Page page) throws Exception{
        return null;
    }

    /**去新增页面
    * @param
    * @throws Exception
    */
    @RequestMapping(value="/goAdd")
    public ModelAndView goAdd()throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.setViewName(PREFIX.concat("/invoiceTitle_edit"));
        mv.addObject("msg", "save");
        mv.addObject("pd", new InvoiceTitle());
        return mv;
    }

    /**去修改页面
    * @param
    * @throws Exception
    */
    @RequestMapping(value="/goEdit")
    public ModelAndView goEdit(@RequestParam Long id)throws Exception{
        ModelAndView mv = this.getModelAndView();
        InvoiceTitle data = invoiceTitleService.selectByPrimaryKey(id);	//根据ID读取
        mv.setViewName(PREFIX.concat("/invoiceTitle_edit"));
        mv.addObject("msg", "edit");
        mv.addObject("pd", data);
        return mv;
    }

    /**
    * 查看详情
    * @param
    * @throws Exception
    */
    @RequestMapping(value="/detail")
    @ResponseBody
    public Object detail(Object id) throws Exception{
        return null;
    }

}